import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    //脚手架
    return Scaffold(
      appBar: AppBar(
        title: Text('GridView'),
        backgroundColor: Colors.blue,
        foregroundColor: Colors.white,
      ),
      //girdview 用来实现宫格布局
      body: buildGridView5(),
    );
  }

  GridView buildGridView5() {
    return GridView.custom(
      padding: EdgeInsets.all(10),
      //SliverGridDelegateWithFixedCrossAxisCount
      gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
        //交叉轴
        crossAxisSpacing: 10,
        //主轴
        mainAxisSpacing: 12,
        //比例
        childAspectRatio: 0.8,
        //限制的是子Item交叉轴 方向的最大宽度
        maxCrossAxisExtent: 100,
      ),
      childrenDelegate: SliverChildBuilderDelegate(
        (BuildContext context, int index) {
          return Container(color: Colors.grey, child: Text('$index'));
        },
        //数量
        childCount: 30,
        //保持 状态
        addAutomaticKeepAlives: true,
        //重绘边界
        addRepaintBoundaries: true,
      ),
    );
  }

  GridView buildGridView3() {
    return GridView.builder(
      itemCount: 10,
      padding: EdgeInsets.all(10),
      gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
        //交叉轴
        crossAxisSpacing: 10,
        //主轴
        mainAxisSpacing: 12,
        //比例
        childAspectRatio: 0.8,
        //限制的是子Item交叉轴 方向的最大宽度
        maxCrossAxisExtent: 200,
      ),
      itemBuilder: (BuildContext context, int index) {
        return Container(color: Colors.grey, child: Text('$index'));
      },
    );
  }

  GridView buildGridView2() {
    return GridView.builder(
      itemCount: 10,
      padding: EdgeInsets.all(10),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        //交叉轴显示Itme数量
        crossAxisCount: 2,
        //交叉轴
        crossAxisSpacing: 10,
        //主轴
        mainAxisSpacing: 12,
        //比例
        childAspectRatio: 0.8,
      ),
      itemBuilder: (BuildContext context, int index) {
        return Container(color: Colors.grey, child: Text('$index'));
      },
    );
  }

  GridView buildGridView1() {
    return GridView.count(
      crossAxisCount: 3,
      //间距 交叉轴
      crossAxisSpacing: 10,
      //主轴
      mainAxisSpacing: 20,
      //
      padding: EdgeInsets.only(left: 12, right: 12, top: 12),
      //设置 子 Item 的比例  宽：高
      childAspectRatio: 1.0,

      children: [
        Container(width: 100, height: 100, color: Colors.grey),
        Container(width: 100, height: 100, color: Colors.grey),
        Container(width: 100, height: 100, color: Colors.grey),
        Container(width: 100, height: 100, color: Colors.grey),
        Container(width: 100, height: 100, color: Colors.grey),
        Container(width: 100, height: 100, color: Colors.grey),
      ],
    );
  }

  GridView buildGridView4() {
    return GridView.extent(
      padding: EdgeInsets.all(10),
      maxCrossAxisExtent: 100,
      //间距 交叉轴
      crossAxisSpacing: 10,
      //主轴
      mainAxisSpacing: 20,
      //
      //设置 子 Item 的比例  宽：高
      childAspectRatio: 1.0,

      children: [
        Container(width: 100, height: 100, color: Colors.grey),
        Container(width: 100, height: 100, color: Colors.grey),
        Container(width: 100, height: 100, color: Colors.grey),
        Container(width: 100, height: 100, color: Colors.grey),
        Container(width: 100, height: 100, color: Colors.grey),
        Container(width: 100, height: 100, color: Colors.grey),
      ],
    );
  }
}
